package com.schoolET.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolET.entity.AdminAndRegion;
import com.schoolET.entity.Region;
import com.schoolET.entity.dto.RegionAdminDTO;
import com.schoolET.entity.vo.AdminAndRegionVO;
import com.schoolET.entity.vo.RegionVO;
import com.schoolET.result.Result;
import com.schoolET.service.IAdminAndRegionService;
import com.schoolET.service.admin.IRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 区域信息 前端控制器
 * </p>
 *
 * @author le
 * @since 2024-05-31
 */
@Api(tags = "区域信息")
@RestController
@RequestMapping("/schoolET/addrMana")
@Slf4j
public class RegionController {

    @Resource
    private IRegionService regionService;

    @Resource
    private IAdminAndRegionService adminAndRegionService;



    /**
     * 1.查询区域列表
     * @param pageNum 页数
     * @param pageSize 每页显示条数
     * @param position 位置（模糊查询）（可选）
     * @return
     */
    @ApiOperationSupport(author = "le")
    @ApiOperation(value = "1.查找区域列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num",value = "页数",example="1",required = true),
            @ApiImplicitParam(name = "size",value = "每页显示条数",example="10",required = true),
            @ApiImplicitParam(name = "position",value = "位置（模糊查询）（可选）",example="北京",required = false,defaultValue = "")
    })
    @SaCheckLogin
    @SaCheckRole("admin")
    @GetMapping("/addrManaList")
    public Result addrManaList(@RequestParam(defaultValue = "1") Integer pageNum,
                                               @RequestParam(defaultValue = "5") Integer pageSize,
                                               @RequestParam(defaultValue = "") String position) {
        pageNum = pageNum < 0 ? 1 : pageNum;
        pageSize = pageSize < 0 ? 5 : pageSize;
        Page<Region> list = regionService.getManaList(pageNum, pageSize, position);
        List<RegionAdminDTO> regionAdminDTOS = BeanUtil.copyToList(list.getRecords(), RegionAdminDTO.class);
        log.info("regionAdminDTOS:{}",regionAdminDTOS);
        regionAdminDTOS.forEach(region ->{
            if (adminAndRegionService.selectByRegionId(region.getId()) != null) {
                region.setAdminName(adminAndRegionService.selectByRegionId(region.getId()).getName());
            }
        });
        Map<String, Object> map = new HashMap<>();
        map.put("records", regionAdminDTOS);
        map.put("total", list.getTotal());
        map.put("current", list.getCurrent());
        map.put("size", list.getSize());
        map.put("hasNext", list.hasNext());
        map.put("hasPrevious", list.hasPrevious());
        return Result.success(map).msg("查询成功");
    }

    /**
     * 2.添加区域
     * @param regionVo
     * @return
     */
    @ApiOperationSupport(author = "le")
    @ApiOperation(value = "2.添加区域")
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/addrManaAdd")
    public Result<String> addrManaAdd(RegionVO regionVo, MultipartFile file) throws IOException {
        regionService.addRegion(regionVo, file);
        return Result.success().msg("添加成功");
    }

    /**
     * 3.删除区域
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "le")
    @ApiOperation(value = "3.删除区域")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "区域id",required = true)
    })
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/addrManaDel/{id}")
    public Result<String> addrManaDel(@PathVariable Integer id) {
        regionService.removeById(id);
        return Result.success().msg("删除成功");
    }

    /**
     * 4.修改区域
     *
     * @param
     * @return
     */
    @ApiOperationSupport(author = "le")
    @ApiOperation(value = "4.修改区域")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "区域id", required = true)
    })
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/addrManaUp/{id}")
    public Result<String> addrManaUp(RegionVO regionVo, @PathVariable Integer id, MultipartFile file) throws IOException {
        regionService.editRegion(regionVo, id, file);
        return Result.success().msg("更新成功");
    }

    @ApiOperationSupport(author = "le")
    @ApiOperation(value = "选择区域管理员（下拉列表）")
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/addrManaAdminId/{id}")
    public Result<String> addrManaAdminId(@RequestBody AdminAndRegionVO adminAndRegionVo,@PathVariable Integer id) {
        AdminAndRegion adminAndRegion = new AdminAndRegion();
        BeanUtil.copyProperties(adminAndRegionVo,adminAndRegion);
        adminAndRegion.setRegionId(id);
//        adminAndRegionService.saveOrUpdate(adminAndRegion);
        adminAndRegionService.addAdminAndRegion(adminAndRegion);
        return Result.success().msg("添加成功");
    }
}